<template>

  <body id="poster">
    <div class="box">
      <div class="login_title">
        <span class="title">管理系统</span>
        <span class="subtitle">Document management system</span>
      </div>
      <div class="login-container">
        <el-form>
          <el-form-item>
            <el-input prefix-icon="el-icon-user"
                      type="text"
                      v-model.trim="loginForm.username"
                      auto-complete="off"
                      placeholder="账号">
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-input prefix-icon="el-icon-lock"
                      :type="passw"
                      v-model.trim="loginForm.password"
                      auto-complete="off"
                      placeholder="密码">
              <i slot="suffix"
                 :class="icon"
                 @click="showPass">
              </i>
            </el-input>
          </el-form-item>

          <el-form-item style="width: 100%">
            <el-button :loading="loading"
                       type="primary"
                       style="width: 100%;background: #505458;border: none"
                       @click="handleLogin">
              <span>登录</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </body>
</template>

<script>
export default {
  name: "Login",
  data () {
    return {
      loading: false,
      loginForm: {
        username: '',
        password: ''
      },
      responseResult: [],
      rules2: {
        name: [{ required: true, message: 'please enter your account', trigger: 'blur' }],
        password: [{ required: true, message: 'enter your password', trigger: 'blur' }]
      },
      checked: false,
      //用于改变Input类型
      passw: "password",
      //用于更换Input中的图标
      icon: "el-input-icon el-icon-view",
    }
  },
  methods: {
    handleLogin () {
      this.loading = true
      this.$store.dispatch('login', this.loginForm).then((res) => {
        if (res) {
          this.$router.push({ path: '/' })
          this.loading = false
        } else {
          this.loading = false
          this.$message.warning("登录失败，请重新登录")
        }
      }).catch(() => {
        this.loading = false
      })
    },
    //密码的隐藏和显示
    showPass () {
      //点击图标是密码隐藏或显示
      if (this.passw === "text") {
        this.passw = "password";
        //更换图标
        this.icon = "el-input-icon el-icon-view";
      } else {
        this.passw = "text";
        this.icon = "el-input-icon el-icon-loading";
      }
    }
  },
  created () {
    // 按 Enter 键登录系统
    document.onkeydown = (e) => {
      e = window.event || e
      if (this.$route.path === '/login' && e.keyCode === 13) this.handleLogin()  // handleLogin() 为登录函数
    }
  },


}
</script>
<style lang="less" scoped>
#poster {
  position: relative;
  box-sizing: border-box;
  background-image: url(../../assets/img/bg.jpeg);
  background-position: center center;
  background-size: cover;
  background-attachment: fixed;
  height: 100%;
  width: 100%;

  .box {
    position: absolute;
    left: 50%;
    top: 45%;
    transform: translate(-50%, -50%);

    .login_title {
      margin: 0 auto 10px auto;
      text-align: center;
      color: #505458;

      .title {
        display: block;
        color: #f1f1f1;
        font-size: 24px;
        letter-spacing: 2px;
        font-family: 黑体;
      }

      .subtitle {
        display: inline-block;
        margin: 5px 0 20px;
        color: lightgrey;
        font-size: 12px;
      }
    }
    .login-container {
      border-radius: 15px;
      background-clip: padding-box;
      margin: 0 auto;
      width: 350px;
      padding: 35px 35px 15px 35px;
      background: #fff;
      border: 1px solid #eaeaea;
      box-shadow: 0 0 25px #cac6c6;
    }
  }
}
</style>
